<?php
class Proxy extends Controller {

	function Proxy()
	{
		parent::Controller();
	    $this->load->model('proxy_model'); 
	}
	
	function index()
	{
	    $this->smarty->view("proxy/single", $data); 
	}
	
	function source($page=0)
	{
	    $this->load->helper ('url');
	    $this->load->library('pagination');
	    $offset=$page;
	    $config['base_url'] = base_url().'/proxy/source/'; 
	    $config['total_rows'] = $this->proxy_model->get_count();
	    $config['per_page'] = 10;
	    $config['uri_segment'] = 3; 
	    $config['full_tag_open'] = '<ul class="page-navigation" >';
	    $config['full_tag_close'] = '</ul>';
	    $config['first_tag_open'] = '<li>';
	    $config['first_tag_close'] = '</li>';
	    $config['last_tag_open'] = '<li>';
	    $config['last_tag_close'] = '</li>';
	    $config['first_url'] = ''; // Alternative URL for the First Page.
	    $config['cur_tag_open'] = '<li>';
	    $config['cur_tag_close'] = '</li>';
	    $config['next_tag_open'] = '<li>';
	    $config['next_tag_close'] = '</li>';
	    $config['prev_tag_open'] = '<li>';
	    $config['prev_tag_close'] = '</li>';
	    $config['num_tag_open'] = '<li>';
	    $config['num_tag_close'] = '</li>';
	    $this->pagination->initialize($config);
	    unset($data);
	    $results=$this->proxy_model->get_items($config['per_page'],$offset);
	    $data['proxys'] = $results;
	    $data['pagination']=$this->pagination->create_links();
	    $this->smarty->view("proxy/source", $data);  
	}
	
	function city($city='',$page=0)
	{
	    $this->load->helper ('url');
	    $this->load->library('pagination');
	    $offset=$page;
	    $config['base_url'] = base_url().'/proxy/city/'; 
	    $config['total_rows'] =$this->proxy_model->get_items_by_key_count("city",$city);
	    $config['per_page'] = 10;
	    $config['uri_segment'] = 3; 
	    $config['full_tag_open'] = '<ul class="page-navigation" >';
	    $config['full_tag_close'] = '</ul>';
	    $config['first_tag_open'] = '<li>';
	    $config['first_tag_close'] = '</li>';
	    $config['last_tag_open'] = '<li>';
	    $config['last_tag_close'] = '</li>';
	    $config['first_url'] = ''; // Alternative URL for the First Page.
	    $config['cur_tag_open'] = '<li>';
	    $config['cur_tag_close'] = '</li>';
	    $config['next_tag_open'] = '<li>';
	    $config['next_tag_close'] = '</li>';
	    $config['prev_tag_open'] = '<li>';
	    $config['prev_tag_close'] = '</li>';
	    $config['num_tag_open'] = '<li>';
	    $config['num_tag_close'] = '</li>';
	    $this->pagination->initialize($config);
	    unset($data);
	    $results=$this->proxy_model->get_items_by_key("city",$city,$config['per_page'],$offset);
	    $data['proxys'] = $results;
	    $data['pagination']=$this->pagination->create_links();
	    $this->smarty->view("proxy/source", $data);  
	}
	
	function submit()
	{
	    $this->smarty->view("proxy/submit", $data); 
	}
	
	
	function check($p)
	{
	    $result=$this->proxy_model->check_proxy($p);
        echo $result;
	    if($result){
	    	echo  '<div class="notice success"> <span><strong>Success:</strong> Success message! hoooraaay!!!!</span> </div>';
	    }else{
	    	echo '<div class="notice warning"> <span><strong>Warning:</strong> This is a warning message. You can use this to warn users on any events.</span> </div>';
	    }
	}
	
	function save()
	{
	    $this->load->model('proxy_model'); 
	    $ip=$this->input->post('ip');
	    $ip=trim($ip);
	    $query=$this->proxy_model->save($ip);
	    if($query){
	    	echo  '<div class="notice success"> <span><strong>Success:</strong> Success message! hoooraaay!!!!</span> </div>';
	    }else{
	    	echo '<div class="notice warning"> <span><strong>Warning:</strong> This is a warning message. You can use this to warn users on any events.</span> </div>';
	    }
	}
	
	function fetch(){
		$url="http://51proxy.org/http_fast.html";
		$proxys= $this->_parse51Proxy($url);
		foreach($proxys as $proxy){			
		    $query=$this->proxy_model->save($proxy);
		    if($query){
		    	echo  '<div class="notice success"> <span><strong>Success:</strong> Success message! hoooraaay!!!!</span> </div>';
		    }else{
		    	echo '<div class="notice warning"> <span><strong>Warning:</strong> This is a warning message. You can use this to warn users on any events.</span> </div>';
		    }
		}
		
	}
	function _parse51Proxy($url){ 
	  $content = @file_get_contents($url);   
	  $proxys=Array();
	  $pattern ='|<td width="30">[^<]*</td>[^<]*<td>([^<]*)</td>[^<]*<td width="60">([^<]*)</td>[^<]*<td width="60">[^<]*</td>|Ui';
	  if(preg_match_all($pattern, $content, $matchs)){
	  	  for($i=0;$i<count($matchs[0]);$i++){
	  	  	$proxys[]=trim($matchs[1][$i].':'.$matchs[2][$i]);
			}
		}
		return $proxys;
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */